package com.lzk.org.controller;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.util.ExcelUtils;
import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.org.domain.Shop;
import com.lzk.org.domain.ShopAuditLog;
import com.lzk.org.dto.ShopDto;
import com.lzk.org.query.ShopQuery;
import com.lzk.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")

@Api(value = "店铺的API",description="店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;


    /**
     * 店铺数量报表数据接口
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }
    /**
     * 导入excel数据接口
     * @param file
     */
    @PostMapping("/import")
    public void importExcel(@RequestPart("file") MultipartFile file){
            //解析excel
        List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shopService.patchInsert(list);
    }

    /**
     * 导出excel数据接口
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //查询所有数据然后导出
            List<Shop> shops = shopService.loadAll();
            //调用工具类List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
            ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    public JsonResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"审核失败");
        }
    }

    /**
     * 审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    public JsonResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"驳回失败");
        }
    }




    /**
     * 商家入驻接口
     * @param shop
     * @return
     */
    @ApiOperation(value = "商家入驻")
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody  Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) {//业务出错
            return JsonResult.me(false,"入驻失败");
        }catch (Exception e){//代码出错
            e.printStackTrace();
            return JsonResult.me(false,"系统异常,请稍后重试");
        }
    }


    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param shopQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryData(shopQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
